import REGION_DATA from 'china-area-data'
// import _ from 'lodash'

/**
 * 根据code获取省/市名
 * @param {string} code 省/市的编码
 */
export function getCityNameByCode(code, data = REGION_DATA) {
  let result = ''
  if (data) {
    const allProvinces = data['86']
    const allProvinceCodes = []

    for (const key in allProvinces) {
      if (allProvinces.hasOwnProperty(key)) {
        // const cityName = allProvinces[key]
        allProvinceCodes.push(key)
      }
    }

    for (const key in data) {
      if (data.hasOwnProperty(key)) {
        // 只遍历省的code
        if (allProvinceCodes.includes(key)) {
          const cities = data[key]
          // 从省里找出匹配的市，赋值给result
          for (const cityKey in cities) {
            if (cities.hasOwnProperty(cityKey)) {
              const city = cities[cityKey]

              if (cityKey === code) {
                result = city
                break
              }
            }
          }
        }
      }
    }
  }
  return result
}
